package com.health.work.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.adapter.QuestionListAdapter;
import com.health.work.model.QuestionBean;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

/**
 * 提问列表页面
 * 
 * @author star
 * 
 */
public class ProblemsListActivity extends Activity {

	public static ArrayList<QuestionBean> questionList;
	private Button back;
	private ListView problemList;
	private String phoneNum;
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private static final int NOASK = 3;
	private static final int SHOWQUESTION = 4;
	private Thread thread;
	protected String errorMessage = "";
	protected QuestionListAdapter adapter;
	private String professionalCategoryId;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				TipsUtil.showAlert(ProblemsListActivity.this, null, "数据请求成功！");
				
				break;
			case FAILE:
				TipsUtil.showAlert(ProblemsListActivity.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(ProblemsListActivity.this, null, "未知错误！");
				break;
			case NOASK:
				TipsUtil.showAlertFinish(ProblemsListActivity.this, null,
						errorMessage);
				break;
			case SHOWQUESTION:
				//显示 questionList 的数据   需要用到listview and adpter 
				adapter=new QuestionListAdapter(ProblemsListActivity.this, questionList, problemList);
				problemList.setAdapter(adapter);
				break;
			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.problems_list_layout);
		initPages();
		getData();
	}

	private void initPages() {

		Bundle bundle=getIntent().getExtras();
		if(bundle!=null){
		professionalCategoryId=(String)bundle.getString("professionalCategoryId");
		}
		back = (Button) findViewById(R.id.btnBack);
		problemList = (ListView) findViewById(R.id.pro_list);
		back.setOnClickListener(onClickListener);

		problemList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				
				Intent intent;
				intent=new Intent(ProblemsListActivity.this, ProblemDetailsActivity.class);
				startActivity(intent);
			}
		});
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;

			default:
				break;
			}
		}

	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 请求网络数据
	 */

	private void getData() {
		final String mobilePhone = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetQuestions(mobilePhone,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								if ("true".equals(result)) {
									handler.sendEmptyMessage(SUCCESS);
								} else if ("false".equals(result)) {
									handler.sendEmptyMessage(FAILE);
								} else if ("0".equals(result)) {
									errorMessage = "暂无数据！";
									 handler.sendEmptyMessage(NOASK);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								Log.e("GetQuestions", object.toString());
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								Log.e("GetQuestions", array.toString());
								if (array != null && array.length() > 0) {
									questionList = new ArrayList<QuestionBean>();
									for (int i = 0; i < array.length(); i++) {
										QuestionBean bean = new QuestionBean();
										bean.Evaluation = array
												.getJSONObject(i).getString(
														"Evaluation");
										bean.EvaluationState = array
												.getJSONObject(i).getString(
														"EvaluationState");
										bean.HealthyCareMobilephone = array
												.getJSONObject(i)
												.getString(
														"HealthyCareMobilephone");
										bean.Id = array.getJSONObject(i)
												.getInt("Id");
										bean.MobilePhone = array.getJSONObject(
												i).getString("MobilePhone");
										bean.ProfessionalCategoryId = array
												.getJSONObject(i).getInt(
														"ProfessionalCategoryId");
										bean.Question = array.getJSONObject(i)
												.getString("Question");
										// bean.QuestionTime=array.getJSONObject(i).getLong("QuestionTime");
										bean.State = array.getJSONObject(i)
												.getString("State");
										System.out.println(bean.Id);
										questionList.add(bean);
									}
									 handler.sendEmptyMessage(SHOWQUESTION);
								}
							}
						});
			}
		});
		thread.start();
	}
	
	
	private void getQuestionData(){
		final String mobilePhoneNum=UserInfoUtils.getUserProperty(this, HEALTHPASSPORT.MOBILEPHONE);
		thread=new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetQuestionsByPCategoryId(1, new DefaultJSONData() {
					
					@Override
					public void parse(String result) throws JSONException {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void parse(JSONObject object) throws JSONException {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void parse(JSONArray array) throws JSONException {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});
	}
}
